package com.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;

import com.gulimall.product.dao.CategoryDao;
import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> all = baseMapper.selectList(null);

        all = dealWithLevel(all);

        return all;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        baseMapper.deleteBatchIds(asList);
    }

    private List<CategoryEntity> dealWithLevel(List<CategoryEntity> all) {

        List<CategoryEntity> collect = all.stream()
                .filter(entry -> entry.getCatLevel() == 1)
                .map(entry -> {
                    entry.setChildren(getChildren(entry, all));
                    return entry;
                })
                .sorted((entry1, entry2) -> entry1.getSort() - entry2.getSort())
                .collect(Collectors.toList());


        return collect;
    }

    private List<CategoryEntity> getChildren(CategoryEntity parent, List<CategoryEntity> categoryEntities) {

        List<CategoryEntity> childrenList = categoryEntities.stream()
                .filter(entry -> parent.getCatId().equals(entry.getParentCid()))
                .map(entry -> {
                    entry.setChildren(getChildren(entry,categoryEntities));
                    return entry;
                })
                .sorted((entry1,entry2) -> entry1.getSort()-entry2.getSort())
                .collect(Collectors.toList());

        return childrenList;
    }


    @Override
    public Long[] getCategoryPath(Long catelogId) {

        ArrayList<Long> list = new ArrayList<>();

        CategoryEntity categoryEntity = this.getById(catelogId);
        getCategoryPath(categoryEntity,list);
        Collections.reverse(list);

        return list.toArray(new Long[list.size()-1]);
    }

    private void getCategoryPath( CategoryEntity categoryEntity, ArrayList<Long> list) {
        list.add(categoryEntity.getCatId());

        if(categoryEntity.getParentCid()!=0L){
            CategoryEntity parentCategoryEntity = this.getById(categoryEntity.getParentCid());
            getCategoryPath(parentCategoryEntity,list);
        }

    }

}